<?php


namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\User as UserModel;
use think\facade\Request;
use think\facade\Session;

class User extends Base
{
    //渲染登录界面
    public function login(){
        $this->view->assign('title','管理员登录');
        return $this->view->fetch('login');
    }

    //验证后台登录
    public function checkLogin(){
        $data = Request::param();
        //查询条件
        $map[] = ['email','=',$data['email']];
        $map[] = ['password','=',sha1($data['password'])];

        $result = UserModel::where($map)->find();
        if($result){
            Session::set('admin_id',$result['id']);
            Session::set('admin_name',$result['name']);
            Session::set('admin_level',$result['is_admin']);
            $this->success('登录成功','admin/user/userList');
        }

        $this->error('登录失败');

    }

    //退出登录
    public function logout(){
//        1.清除session
        Session::clear();
//        2.退出登录并跳转到登录页面
        $this->success('退出成功','user/login');

    }

    //用户列表
    public function userList(){
        //1.获取当前用户id和级别
        $data['admin_id'] = Session::get('admin_id');
        $data['admin_level'] = Session::get('admin_level');

//        halt($data);
        //2  3.1如果是超级管理员，获取到全部信息
        if($data['admin_level']==1){
            $userList = UserModel::select();
        }else{
            //3.2否则获取当前用户信息
            $userList = UserModel::where('id',$data['admin_id'])->select();
        }



        //4.模板赋值
        $this->view->assign('title','用户管理');
        $this->view->assign('empty','<span style="color: red;">没有任何数据</span>');

        $this->view->assign('userList',$userList);

        //5.渲染出用户模板
        return $this->fetch('userList');

    }

    //渲染用户编辑界面
    public function userEdit(){
        //1.获取要更新的用户主键
        $userId = Request::param('id');
        //2.根据主键进行查询
        $userInfo = UserModel::where('id',$userId)->find();
        //渲染编辑界面的目标变量
        $this->assign('title','编辑用户');
        $this->assign('userInfo',$userInfo);
        //4.渲染出编辑界面
        return $this->view->fetch('useredit');

    }

    //执行用户信息的保存
    public function doEdit(){
        //1.获取用户提交的信息
        $data = Request::param();

        //2.取出主键
        $id = $data['id'];
        //3.将密码加密
        if($data['password'] == $this->password){
            unset($data['password']);
        }else{
            $data['password'] = sha1($data['password']);
        }

        //4.输出data中的id
        unset($data['id']);

        //5.执行更新操作
        if(UserModel::where('id',$id)->data($data)->update()){
            return $this->success('更新成功','userList');
        }
        $this->error('更新失败');

    }

    //执行用户的删除操作
    public function doDelete(){
        //1.获取要删除的id
        $id = Request::param('id');
//        halt($id);

        //2.执行删除
        if(UserModel::where('id',$id)->delete()){
            return $this->success('删除成功','userList');
        }
        $this->error('删除失败');
    }
}